package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.PartsManagement.TeamPersonnelMapping;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.lbj.ComponentSingleRegistrationTable;
import com.cn.bean.pojo.workOrder.BusinessItemWorkOrderPool;
import com.cn.bean.pojo.workOrder.WorkOrderPlanMainTable;
import com.cn.bean.pojo.workOrder.WorkOrderPlanSubTable;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.WorkOrderPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class WorkOrderPlanController {
    @Autowired
    private WorkOrderPlanService workOrderPlanService;
    @RequestMapping("/select/WorkOrderPlanMainTable")
    @ResponseBody
    public List<WorkOrderPlanMainTable> selectWorkOrderPlan(@RequestBody WorkOrderPlanMainTable workOrderPlanMainTable) {
        List<WorkOrderPlanMainTable> assists = workOrderPlanService.selectWorkOrderPlan(workOrderPlanMainTable);
        return assists;
    }
    @RequestMapping("/select/WorkOrderPlanMainTable/All")
    @ResponseBody
    public List<WorkOrderPlanMainTable> selectWorkOrderPlanAll(@RequestBody WorkOrderPlanMainTable workOrderPlanMainTable) {
        List<WorkOrderPlanMainTable> assists = workOrderPlanService.selectWorkOrderPlanAll(workOrderPlanMainTable);
        return assists;
    }
    @RequestMapping("/select/WorkOrderPlanSub")
    @ResponseBody
    public List<WorkOrderPlanSubTable> selectWorkOrderPlanSub(@RequestBody WorkOrderPlanSubTable subTable) {
        List<WorkOrderPlanSubTable> sub = workOrderPlanService.selectWorkOrderPlanSub(subTable);
        return sub;
    }
    @RequestMapping("/select/WorkUser")
    @ResponseBody
    public List<User> selectWorkUser(@RequestBody User user) {
        List<User> sub = workOrderPlanService.selectUser(user);
        return sub;
    }
    @RequestMapping("/select/WorkTeam")
    @ResponseBody
    public List<TeamPersonnelMapping> selectWorkUser(@RequestBody TeamPersonnelMapping teamPersonnelMapping) {
        List<TeamPersonnelMapping> sub = workOrderPlanService.selectWorkTeam(teamPersonnelMapping);
        return sub;
    }
    @RequestMapping("/select/WorkOrderPool")
    @ResponseBody
    public List<BusinessItemWorkOrderPool> selectWorkOrderPool(@RequestBody BusinessItemWorkOrderPool businessItemWorkOrderPool) {
        List<BusinessItemWorkOrderPool> sub = workOrderPlanService.selectWorkOrderPool(businessItemWorkOrderPool);
        return sub;
    }
    @RequestMapping("/insert/WorkOrderPlan")
    @ResponseBody
    public Result insertWorkOrderPlan(@RequestBody JSONObject jsonObject)  {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        WorkOrderPlanMainTable mainTable = new WorkOrderPlanMainTable();
        BusinessItemWorkOrderPool workOrderPool=new BusinessItemWorkOrderPool();
        Integer id = workOrderPlanService.getWorkOrderPlanMainAutoid();
        Integer workid=workOrderPlanService.getWorkOrderPoolAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        workid = workid != null ? ++workid : 1;
        String userName = jsonObject.getString("userName");
        int planType=jsonObject.getInteger("planType");
        int grindingStandardMid=jsonObject.getInteger("grindingStandardMid");
        int djStatus = 0;
        String equipment=jsonObject.getString("equipment");
        Integer workOrderType=jsonObject.getInteger("workOrderType");
        int equipmentId=jsonObject.getInteger("equipmentId");
        Integer processingMethod=jsonObject.getInteger("processingMethod");
        String djTime=jsonObject.getString("djTime");
        String grindingStandardName=jsonObject.getString("grindingStandardName");
        Integer departmentId=jsonObject.getInteger("departmentId");
        Integer employeeId=jsonObject.getInteger("employeeId");
        String  measurementUnit=jsonObject.getString("measurementUnit");
        Integer  processingShift=jsonObject.getInteger("processingShift");
        String   processingDay=jsonObject.getString("processingDay");
        Integer  processingWorkerId=jsonObject.getInteger("processingWorkerId");
        Integer  processingWorkTeamId=jsonObject.getInteger("processingWorkTeamId");
        Integer  singlequantity=jsonObject.getInteger("singlequantity");
          if (measurementUnit=="组"){
              Integer groupquantity=jsonObject.getInteger("singlequantity");
              mainTable.setGroupquantity(groupquantity);
          }
        int groupquantity=0;
        mainTable.setId(id);
        mainTable.setPlanType(planType);
        mainTable.setGrindingStandardMid(grindingStandardMid);
        mainTable.setEquipment(equipment);
        mainTable.setCjr(userName);
        mainTable.setCjsj(format.format(date));
        mainTable.setDjNumber(id);
        mainTable.setWorkOrderType(workOrderType);
        mainTable.setDjTime(djTime);
        mainTable.setWorkOrderCode(String.valueOf(id));
        mainTable.setEquipmentId(equipmentId);
        mainTable.setProcessingMethod(processingMethod);
        mainTable.setGrindingStandardName(grindingStandardName);
        mainTable.setDepartmentId(departmentId);
        mainTable.setGroupquantity(groupquantity);
        mainTable.setEmployeeId(employeeId);
        mainTable.setProcessingShift(processingShift);
        mainTable.setProcessingDay(processingDay);
        mainTable.setSinglequantity(singlequantity);
        mainTable.setProcessingWorkTeamId(processingWorkTeamId);
        mainTable.setProcessingWorkerId(processingWorkerId);
        mainTable.setMeasurementUnit(measurementUnit);
        workOrderPool.setId(workid);
        workOrderPool.setWorkOrderPlanMainTableId(id);
        workOrderPool.setWorkOrdeStatus(0);
        workOrderPool.setWorkOrdeType(workOrderType);
        workOrderPool.setCjr(userName);
        workOrderPool.setCjsj(format.format(date));
        workOrderPool.setWorkOrderCode(String.valueOf(id));
        workOrderPool.setEquipmentId(equipmentId);
        workOrderPool.setGrindingStandardMid(grindingStandardMid);
        workOrderPool.setProcessingMethod(processingMethod);
        workOrderPool.setEquipmentName(equipment);
        workOrderPool.setEquipmentCode(String.valueOf(equipmentId));
        int a = workOrderPlanService.insertWorkOrderPlan(mainTable);
        int c=workOrderPlanService.insertWorkOrderPool(workOrderPool);
        if (a != 1&&c!=1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<WorkOrderPlanSubTable> djBhList = dataArray.toJavaList(WorkOrderPlanSubTable.class);
            for (WorkOrderPlanSubTable dataStructureDto : djBhList) {
                WorkOrderPlanSubTable subTable = new WorkOrderPlanSubTable();
                ComponentSingleRegistrationTable componentSingleRegistrationTable=new ComponentSingleRegistrationTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = workOrderPlanService.getWorkOrderPlanSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                componentSingleRegistrationTable.setUniqueCode(subTable.getUniqueCode());
                componentSingleRegistrationTable.setLockTable("工单计划表");
                int b = workOrderPlanService.insertWorkOrderPlanSub(subTable);
                int d =workOrderPlanService.lockWorkOrder(componentSingleRegistrationTable);
                if (b != 1&&d!=1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success(200);
    }


    @RequestMapping("/select/BusinessItemWorkOrderPool")
    @ResponseBody
    public List<BusinessItemWorkOrderPool> selectBusinessItemWorkOrderPool(@RequestBody JSONObject jsonObject) {
        String workOrderCode = jsonObject.getString("input1");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        workOrderCode = (workOrderCode != null) ? workOrderCode : "";
        return workOrderPlanService.selectBusinessItemWorkOrderPool(workOrderCode, page, limit);
    }
    @RequestMapping("/select/BusinessItemWorkOrderPoolId")
    @ResponseBody
    public List<BusinessItemWorkOrderPool> selectBusinessItemWorkOrderPoolId(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        return workOrderPlanService.selectBusinessItemWorkOrderPoolId(id);
    }
    @RequestMapping("/select/WorkOrderPlanSubTable")
    @ResponseBody
    public List<WorkOrderPlanSubTable> selectWorkOrderPlanSubTable(@RequestBody JSONObject jsonObject) {
        Integer mid = jsonObject.getInteger("id");
        return workOrderPlanService.selectWorkOrderPlanSubTable(mid);
    }
    @RequestMapping("/update/WorkOrderAudit")
    @ResponseBody
    public Result updateWorkOrderAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                workOrderPlanService.updateWorkOrderAudit(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
    @RequestMapping("/del/WorkOrderAudit")
    @ResponseBody
    public Result delWorkOrderAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                workOrderPlanService.delWorkOrderAudit(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
    @RequestMapping("/del/WorkOrder")
    @ResponseBody
    public Result delWorkOrder(@RequestBody JSONObject jsonObject){
//        JSONArray djBhArray = jsonObject.getJSONArray("uniqueCode");
        String uniqueCode = jsonObject.getString("uniqueCode");
            int a=workOrderPlanService.openWorkOrder(uniqueCode);
            if (a!=1){
                return Result.failure("500", "审核失败");
            }
            return Result.success(200);
    }
    @RequestMapping("/del/WorkOrderAll")
    @ResponseBody
    public Result delWorkOrderAll(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("uniqueCode");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<String> djBhList = djBhArray.toJavaList(String.class);
            for (String uniqueCode : djBhList) {
                workOrderPlanService.openWorkOrder(uniqueCode);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
}
